import {
	httpTokenRequest
} from '@/utils/http.js';
export default {
	data() {
		return {
			showLoad: false,
			type: '',
			menuList: [{
				icon: '/static/img/wenTiKu/wx.png',
				name: '微信好友',
				remark: '',
				url: '',
				handler: 'shareTo'
			},
			{
				icon: '/static/img/wenTiKu/pyq.png',
				name: '朋友圈',
				remark: '',
				url: '',
				handler: 'shareToMoments'
			},
			{
				icon: '/static/img/wenTiKu/qq.png',
				name: 'QQ好友',
				remark: '',
				url: '',
				handler: 'toShareQq'
			},
			{
				icon: '/static/img/wenTiKu/copyLink.png',
				name: '复制链接',
				remark: '',
				url: '',
				handler: 'copyLink'
			},
			{
				icon: '/static/img/wenTiKu/collect_0.png',
				name: '收藏',
				remark: '',
				url: '',
				handler: 'collect'
			}, {
				icon: '/static/img/wenTiKu/fontSize.png',
				name: '阅读字号',
				remark: '',
				url: '',
				handler: 'changShowFontSize'
			}],
			details: {},
			sourceList: [],
			showMore: false,
			shareId: null,
			showFontSize: false,
			fontSizeIndex: 0,
			fontSize: '28rpx',
			showParse: true,
			url1: '/pages2/policyZhunzeInfo/policyZhunzeInfo',//税收法规库微信好友
			url2: '/page3/shuishouInfo/shuishouInfo',//优惠政策微信好友
			url3: '/pages/details/policyZhunzeInfo',//税收法规库微信朋友圈,qq,复制
			url4: '/pages/details/shuishouDetail',//优惠政策微信朋友圈,qq,复制

		};
	},
	methods: {
		//获取详细信息
		getInfo(id) {
			uni.showToast({
				title: '加载中',
				icon: 'loading'
			});
			let data = {
				id: id
			}
			if (this.type != '') {
				data.type = this.type
			}
			httpTokenRequest({
				url: this.$paths.getInfoPolicy,
				method: 'POST',
				params: data
			})
				.then((res) => {
					if (res.code == 1) {
						uni.hideLoading();
						console.log(res);
						this.details = res.data;
						this.menuList[4].icon = this.details.is_collect == 1 ? '/static/img/wenTiKu/collect_1.png' : '/static/img/wenTiKu/collect_0.png';
						this.showLoad = true
					} else {
						this.$tools.showToast(res.msg);
					}
				})
				.catch((err) => {
					console.log('错误回调', err);
				});
		},

		//关闭弹框
		close() {
			this.showMore = false;
		},

		//点击更多中的菜单
		menuClick(item) {
			if (item.handler) {
				this[item.handler]();
			} else if (item.url) {
				this.$used.href(item.url);
			}
		},

		//微信好友
		shareTo() {
			var that = this
			let url = this.type == '' ? this.url1 : this.url2;
			let code = uni.getStorageSync('userInfo').invite_code;
			let obj = {
				id: 'gh_d8bf9808c336',
				path: url + '?id=' + that.shareId + '&code=' + code,
				webUrl: that.$h5Url.server + '/pages/downLoad/downLoad'
			};
			uni.share({
				provider: 'weixin',
				scene: 'WXSceneSession',
				type: 5,
				miniProgram: obj,
				imageUrl: '/static/logo-min.png',
				title: that.details.title,
				success: function (res) {
					console.log('success:' + JSON.stringify(res));
					that.showMore = false;
				},
				fail: function (err) {
					console.log('fail:' + JSON.stringify(err));
				}
			});
		},

		//分享朋友圈
		shareToMoments() {
			var that = this
			let url = this.type == '' ? this.url3 : this.url4;
			var share_url = this.$h5Url.server + url + '?id=' + this.shareId;
			uni.share({
				provider: 'weixin',
				scene: 'WXSceneTimeline',
				type: 0,
				href: share_url,
				title: that.details.title,
				summary: that.removeTextTag().substring(0, 100),
				imageUrl: '/static/logo-min.png',
				success: function (res) {
					console.log('success:' + JSON.stringify(res));

				},
				fail: function (err) {
					console.log('fail:' + JSON.stringify(err));
				}
			});
		},

		//分享QQ
		toShareQq() {
			var that = this
			let url = this.type == '' ? this.url3 : this.url4;
			var share_url = this.$h5Url.server + url + '?id=' + this.shareId;
			uni.share({
				provider: 'qq',
				scene: 'WXSceneSession',
				type: 0,
				href: share_url,
				title: that.details.title,
				summary: that.removeTextTag().substring(0, 100),
				imageUrl: '/static/logo-min.png',
				success: function (res) {
					console.log('success:' + JSON.stringify(res));
					that.showMore = false;
				},
				fail: function (err) {
					console.log('fail:' + JSON.stringify(err));
				}
			});
		},


		//复制链接
		copyLink() {
			var that = this;
			let url = this.type == '' ? this.url3 : this.url4;
			uni.setClipboardData({
				data: that.$h5Url.server + url + '?id=' + that.shareId,
				success: function () {
					uni.showToast({
						title: '链接复制成功'
					});
					that.showMore = false;
					uni.showToast({
						title: '链接复制成功'
					});
				}
			});
		},


		//收藏 or 取消收藏
		collect() {
			httpTokenRequest({
				method: 'POST',
				url: this.$paths.changeCollect,
				params: {
					id: this.details.id
				}
			})
				.then((res) => {
					if (res.code == 1) {
						//收藏和未收藏切换
						this.details.is_collect = 1 - this.details.is_collect;
						console.log(this.details.is_collect);
						this.menuList[4].icon = `/static/img/wenTiKu/collect_${this.details.is_collect}.png`;
						if (this.details.is_collect == 1) {
							this.$tools.showToast('取消收藏');
							this.menuList[4].icon = `/static/img/wenTiKu/collect_0.png`;
						} else if (this.details.is_collect == 0) {
							this.menuList[4].icon = `/static/img/wenTiKu/collect_1.png`;
							this.$tools.showToast('收藏成功');
						}
						setTimeout(() => {
							this.showMore = false;
						}, 1000);
					} else {
						this.$tools.showToast(res.msg);
					}
				})
				.catch((err) => {
					console.log('错误回调', err);
				});
		},

		// 切换字体弹出
		changShowFontSize() {
			this.showFontSize = !this.showFontSize
			this.showMore = false
		},

		// 切换字体
		changFontSize(index) {
			this.fontSizeIndex = index
			switch (index) {
				case 0:
					this.fontSize = '28rpx'
					break
				case 1:
					this.fontSize = '36rpx'
					break
				case 2:
					this.fontSize = '46rpx'
					break
			}
			this.showParse = false
			this.$nextTick(() => {
				this.showParse = true
			})
		},


		//格式化富文本内容
		removeTextTag(str) {
			return str = this.details.info_content.replace(/<[^<>]+>/g, "").replace(/&nbsp;/gi, "");
		},
	},
	computed: {
		'fontSizeLeft': function () {
			return `primary${this.fontSizeIndex + 1}`
		}
	},
	onLoad(e) {
		let { id, type } = e
		this.type = type ? type : '';
		this.getInfo(id);
		this.shareId = id;
	},
	onReady() {
	},
}